package com.levy.contronller.lamdaTest;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Optional;

public class OptionalTest {

    public static void main(String[] args) {
//        Optional<String> option = Optional.empty();
//        Optional<String> option = Optional.of("hello");

//        if (option.isPresent()) {
//            System.out.println(option.get());
//        }

//        //推荐写法
//        option.ifPresent(o -> System.out.println(o));
//
//        System.out.println(option.orElse("world"));
//
//        System.out.println(option.orElseGet(() -> "xxx"));

        Emp emp = new Emp();
        emp.setUserName("zhangsan");

        Emp emp2 = new Emp();
        emp2.setUserName("lisi");

        Company company = new Company();
        company.setName("myconmay");

        List<Emp> list = Arrays.asList(emp, emp2);
//        company.setList(list);

        Optional<Company> optionalCompany = Optional.ofNullable(company);
        List<Emp> emps = optionalCompany.map(o -> o.getList()).orElse(Collections.emptyList());
        System.out.println(emps);


    }
}


class Company {
    private String name;

    private List<Emp> list;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public List<Emp> getList() {
        return list;
    }

    public void setList(List<Emp> list) {
        this.list = list;
    }
}

class Emp {
    private String userName;

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }
}